import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:trip_flutter/dao/login_dao.dart';
import 'package:trip_flutter/util.dart';
import 'package:trip_flutter/util/string_util.dart';
import 'package:trip_flutter/widget/input_widget.dart';
import 'package:trip_flutter/widget/login_widget.dart';
import 'package:url_launcher/url_launcher.dart';

import '../util/route_util.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  bool loginEnable = false;
  String? userName;
  String? password;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [..._background(), _content()],
      ),
    );
  }

  _background() {
    return [
      Positioned.fill(
          child: Image.asset(
        "images/login-bg1.jpg",
        fit: BoxFit.cover,
      )),
      Positioned.fill(
          child: Container(
        decoration: const BoxDecoration(color: Colors.black45),
      ))
    ];
  }

  _content() {
    return Positioned.fill(
        left: 25,
        right: 25,
        child: ListView(
          children: [
            hiSpace(height: 80),
            const Text(
              "账号密码登录",
              style: TextStyle(fontSize: 30, color: Colors.white),
            ),
            hiSpace(height: 30),
            InputWidget(
              "请输入账号",
              valueChanged: (text) {
                userName = text;
                _checkInput();
              },
            ),
            const Divider(
              height: 1,
              thickness: 1,
              color: Colors.white,
            ),
            hiSpace(height: 20),
            InputWidget(
              "请输入密码",
              obscureText: true,
              valueChanged: (text) {
                password = text;
                _checkInput();
              },
            ),
            const Divider(
              height: 1,
              thickness: 1,
              color: Colors.white,
            ),
            hiSpace(height: 50),
            LoginButton(
              "登录",
              enable: loginEnable,
              voidCallback: _login,
            ),
            hiSpace(height: 15),
            Align(
              alignment: Alignment.centerRight,
              child: InkWell(
                onTap: _jumpRegister,
                child: const Text(
                  "注册账号",
                  style: TextStyle(color: Colors.white),
                ),
              ),
            )
          ],
        ));
  }

  void _checkInput() {
    bool enable;
    if (isNotEmpty(userName) && isNotEmpty(password)) {
      enable = true;
    } else {
      enable = false;
    }

    setState(() {
      loginEnable = enable;
      if (kDebugMode) {
        print("----------------------值为: $loginEnable");
      }
    });
  }

  _jumpRegister() async {
    Uri uri = Uri.parse("https://api.geekailab.com/uapi/"
        "swagger-ui.html#/Account/registrationUsingPOST");

    if (await canLaunchUrl(uri)) {
      await launchUrl(uri);
    } else {
      throw "Could not launch $uri";
    }
  }

  void _login() async {
    if (loginEnable) {
      LoginDao.login(userName!, password!);
      NavigatorUtil.goHomePage(context);
    }
  }
}
